/*------------------------------------------------------------------------
* Seedset-generator
* $Id$
* Copyright (c) 2008 Regents of the University of Minnesota.
* All Rights Reserved by the Regents of the University of Minnesota.
* This file contains Original Code and/or Modifications of Original
* Code as defined in the MAGNET Public Source License, which is
* available at http://www.cs.umn.edu/magnet/source_license_v1.txt.
* You may not use this file except in compliance with the License.
*------------------------------------------------------------------------
*/

package edu.umn.cs.tac.seedsetgen;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.lang.Long;

public class SeedInfoBox extends JPanel implements ActionListener
{
    SeedParameter currentSeedParameter; //the seedParameter the box is currently focusing on.
    SeedTree mainTree;

    JLabel nameLabel;
    JLabel descriptionLabel;
    JLabel groupLabel;
    JLabel controlLabel;
    JLabel seedLabel;

    JTextField nameBox;
    JTextField descriptionBox;
    JTextField groupBox; //Parameter or a group of parameters
    JTextField controlBox; //Random, Controlled, or Partially Controlled
    JTextField seedBox;

    private static String SEED_COMMAND = "seed";

    SeedInfoBox(SeedTree seedTree)
    {
        mainTree = seedTree;
        nameLabel = new JLabel("Name");
        descriptionLabel = new JLabel("Description");
        groupLabel = new JLabel("Parameter Type");
        controlLabel = new JLabel("Control Type");
        seedLabel = new JLabel("Seed (0=random)");
        seedLabel.setVisible(false);

        nameBox = new JTextField();
        nameBox.setEditable(false);

        descriptionBox = new JTextField();
        descriptionBox.setEditable(false);

        groupBox = new JTextField();
        groupBox.setEditable(false);

        controlBox = new JTextField();
        controlBox.setEditable(false);

        seedBox = new JTextField();
        seedBox.setEditable(true); //TODO: Make this editable for manual seed entry.
        seedBox.setVisible(false);
        seedBox.setActionCommand(SEED_COMMAND);
        seedBox.addActionListener(this);

        setLayout(new GridLayout(0,2));
        add(nameLabel);
        add(nameBox);
        add(groupLabel);
        add(groupBox);
        //add(descriptionLabel);
        //add(descriptionBox);
        add(controlLabel);
        add(controlBox);
        add(seedLabel);        
        add(seedBox);

    }

    // Accessor methods
    public void setCurrentSeedParameter(SeedParameter param)
    {
        currentSeedParameter = param;
    }

    public SeedParameter getCurrentSeedParameter()
    {
        return currentSeedParameter;
    }



    public void actionPerformed(ActionEvent evt)
    {
        String command = evt.getActionCommand();
        String text;		

        if (SEED_COMMAND.equals(command))
        {
            //Entered seed info
            text = seedBox.getText();		
            boolean setToControl = true;
            long seed;
            try
            {
                seed = Long.parseLong(text.trim());
                //System.out.println("seed = " + seed);
            }
            catch (NumberFormatException nfe)
            {
                System.out.println("NumberFormatException: " + nfe.getMessage());
                System.out.println("Setting to random by default.");
                seed = (long)0;
            }

            if (seed == ((long) 0) )
            {
                setToControl = false;
            }

            mainTree.setNodeControl(setToControl, seed);
            mainTree.updateGraphicalInfo();

            //currentSeedParameter.setSeed(text);
        }	
    }


    public void printInfo(SeedParameter seedParameter)
    {
        nameBox.setText(seedParameter.getName());
        groupBox.setText(seedParameter.getParameterGroupAsString());
        descriptionBox.setText(seedParameter.getDescription());
        controlBox.setText(seedParameter.getControlTypeAsString());
        seedBox.setText(Long.toString(seedParameter.getSeed()));

        if (seedParameter.getParameterGroup())
        { //group of parameters
            seedLabel.setVisible(false);
            seedBox.setVisible(false);
        }
        else
        {
            seedLabel.setVisible(true);
            seedBox.setVisible(true);
        }
    }



}
